﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;
using Qianwei.Common;

namespace Qianwei.Web.Admins
{
    public partial class TopPictureManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region Method
        private void BindData()
        {
            IList<home_page_picture> hpplist = HomePagePictureManager.Instance.QueryHomePagePicture(null, "Sort_Id");
            r_toppics.DataSource = hpplist;
            r_toppics.DataBind();
        }

        private string CheckInput()
        {
            string strErr = string.Empty;
            if (txtSortId.Text.Trim().Length == 0)
            {
                strErr = "请输入排序号";
            }
            else if (txtPictureDes.Text.Trim().Length == 0)
            {
                strErr = "请输入图片描述";
            }
            else if (FileUpload.Value.Trim().Length == 0)
            {
                strErr = "请选择要上传的图片";
            }
            else if (!Util.IsInteger(txtSortId.Text.Trim()))
            {
                strErr = "请输入正确的排序号";
            }
            return strErr;
        }

        private void SavePicture()
        {
            home_page_picture hpp = new home_page_picture();
            hpp.Create_Time = DateTime.Now;
            hpp.Last_Change_Time = hpp.Create_Time;
            hpp.Page_Content = "";
            hpp.Picture_Des = txtPictureDes.Text.Trim();
            hpp.Picture_Path = string.Format("FirstPage/{0}", Path.GetFileName(FileUpload.PostedFile.FileName));
            hpp.Sort_Id = txtSortId.Text.Trim().TryInt();

            string webPath = Server.MapPath("~/");
            if (!File.Exists(string.Format("{0}\\{1}", webPath, hpp.Picture_Path)))
            {
                FileUpload.PostedFile.SaveAs(string.Format("{0}\\{1}", webPath, hpp.Picture_Path));
            }

            if (HomePagePictureManager.Instance.Add(hpp) > 0)
            {
                Util.PageAlert("保存成功");
            }
            else
            {
                Util.PageAlert("对不起,保存失败");
            }
            BindData();
        }
        #endregion

        #region Event

        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (HomePagePictureManager.Instance.Delete(((LinkButton)sender).CommandArgument.TryInt(), Server.MapPath("~/")))
            {
                BindData();
                Util.PageAlert("删除成功");
            }
            else
            {
                Util.PageAlert("对不起,删除失败");
            }
        }

        protected void FileUpload_Click(object sender, EventArgs e)
        {
            string result = CheckInput();
            if (result.Length > 0)
            {
                Util.IsInteger(result);
            }
            else
            {
                SavePicture();
            }
        }
        #endregion
    }
}